package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Category;
import com.itheima.service.ICategoryService;
import com.itheima.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;


    @PostMapping
    public Result addCategory(@RequestBody Category category){
        return categoryService.addCategory(category);
    }

    @PutMapping
    public Result updateCategory(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    @GetMapping("/list")
    public Result list(){

        return categoryService.seleteList();
    }



    @GetMapping("/page")
    public Result getPage(@RequestParam(value = "page") int page, @RequestParam(value = "pageSize")  int pageSize){
        IPage categoryServicePage = categoryService.getPage(page, pageSize );
        if (page >categoryServicePage.getPages()){
            categoryServicePage = categoryService.getPage((int) categoryServicePage.getPages(), pageSize);
        }
        return Result.success(categoryServicePage);
    }
}
